package com.njbw.design_pattern.created.prototype.shallowCopy;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 浅拷贝——直接赋值
 * 播放列表
 *
 * @author 黄侯淳
 * @since JDK 1.8
 */
@Slf4j
@Data
public class PlayList {

    /**
     * 播放列表ID
     */
    private Long id;

    /**
     * 播放列表名称
     */
    private String name;

    /**
     * 歌曲列表
     */
    private List<Song> songList;

    public PlayList() {
        this.songList = new ArrayList<>();
    }

    /**
     * 拷贝构造函数
     * 用于浅拷贝的构造器
     *
     * @param sourcePlayList 源播放列表
     */
    public PlayList(PlayList sourcePlayList) {
        // 直接赋值，浅拷贝
        this.id = sourcePlayList.getId();
        this.name = sourcePlayList.getName();
        this.songList = sourcePlayList.getSongList();
    }

    public void addSong(Song song) {
        this.songList.add(song);
    }

    public static void main(String[] args) {
        PlayList playList = new PlayList();
        playList.setId(1L);
        playList.setName("周杰伦");
        playList.addSong(new Song("稻香", "周杰伦"));
        playList.addSong(new Song("七里香", "周杰伦"));
        playList.addSong(new Song("告白气球", "周杰伦"));

        // 浅拷贝后的最喜爱的专辑
        PlayList favouriteList = new PlayList(playList);
        favouriteList.setName("最爱");
        favouriteList.addSong(new Song("曹操", "林俊杰"));
        log.info("favouriteList: {}", favouriteList);
    }
}
